﻿/*
	SWFProgressWatcher
	
	Checks to see when the Captivate movie is done
	and emits an event for it.
	
    Created by Jesse R. Warden a.k.a. "JesterXL"
	jesterxl@jessewarden.com
	http://www.jessewarden.com
	
	This is release under a Creative Commons license. 
    More information can be found here:
    
    http://creativecommons.org/licenses/by/2.5/
*/
import mx.core.UIObject;

class com.jxl.captivate.SWFProgressWatcher extends UIObject
{
	static var symbolName:String = "com.jxl.captivate.SWFProgressWatcher";
	static var symbolOwner:Object = com.jxl.captivate.SWFProgressWatcher;
	public var className:String = "SWFProgressWatcher";
	
	private var process_mc:MovieClip;
	
	function SWFProgressWatcher()
	{
	}
	
	private function init():Void
	{
		super.init();
		
		this.tabEnabled = false;
		this.tabChildren = false;
		
		stopAllWatching();
	}
	
	private function createChildren():Void
	{
		createEmptyMovieClip("process_mc", 0);
	}
	
	public function stopAllWatching():Void
	{
		process_mc.removeMovieClip();
		createEmptyMovieClip("process_mc", 0);
	}
	
	public function startWatching(mc:MovieClip):Number
	{
		var d:Number = process_mc.getNextHighestDepth();
		var ref_mc:MovieClip = process_mc.createEmptyMovieClip("p" + d, d);
		ref_mc.mc = mc;
		ref_mc.onEnterFrame = function()
		{
			var c:Number = this.mc._currentframe;
			var t:Number = this.mc._totalframes;
			if(!isNaN(c))
			{
				if(c != null)
				{
					if(!isNaN(t))
					{
						if(t != null)
						{
							//_global.debug("-------------");
							//_global.debug("this.mc.cpSkinLoader_mc.rdinfoFrameCount: " + this.mc.cpSkinLoader_mc.rdinfoFrameCount);
							//_global.debug("this.mc.cpSkinLoader_mc.rdinfoCurrentFrame: " + this.mc.cpSkinLoader_mc.rdinfoCurrentFrame);
							var max:Number;
							// As of Captivate 2.0.0
							if(this.mc.cpSkinLoader_mc != null)
							{
								max = this.mc.cpSkinLoader_mc.rdinfoFrameCount;
							}
							else
							{
								// As of Captivate 1.0.0
								max = this.mc.rdinfoFrameCount;
							}
							
							if(max == undefined) return;
							
							var current:Number;
							if(this.mc.cpSkinLoader_mc != null)
							{
								current = this.mc.cpSkinLoader_mc.rdinfoCurrentFrame;
							}
							else
							{
								current = this.mc.rdinfoCurrentFrame;
							}
							
							if(current == undefined) return;
							
							//trace("--------");
							//_global.debug("current: " + current);
							//_global.debug("max: " + max);
							if(current == max)
							{
								this._parent._parent.onMCDone(this, this.mc, c, t);
							}
						}
					}
				}
			}
		};
		return d;
	}
	
	public function stopWatching(id:Number):Boolean
	{
		var mc:MovieClip = process_mc.getInstanceAtDepth(id);
		if(mc != null)
		{
			mc.removeMovieClip();
			return true;
		}
		else
		{
			return false;
		}
	}
	
	private function onMCDone(watcherMC:MovieClip, watchedMC:MovieClip, currentFrame:Number, totalFrames:Number):Void
	{
		dispatchEvent({type: "onMovieDone", target: this, 
					  id: watcherMC.getDepth(), watchedMC: watchedMC,
					  currentFrame: currentFrame, totalFrames: totalFrames});
	}
	
}